const BehaviorAll = require("../../../behaviors/behavior_all.js");
const ServiceUser = require("../../../service/service_user.js");
const ServiceOrderManage = require("../../../service/service_orderManage.js");

Component({
  /**
   * behaviors
   */
  behaviors: [BehaviorAll],
  /**
   * 页面的初始数据
   */
  data: {
    /**
     * 订单数据格式
        {
          id: 1,
          name: "法国进口红酒拉斐天使庄园干红葡萄酒红酒整箱礼盒装750ml*6",
          price: 100,
          imgUrl:''
        }
     */
    order: null,
    /**
     * 指派列表
     */
    assignPage: [],
    assignPageShowFlag: false,
    /**
     * 用户信息
     */
    userInfo: null
  },

  methods: {
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      const _this = this;
      const orderId = options.orderId;
      ServiceUser.getInfo(function (data) {
        _this.setData({
          userInfo: data
        })
        // 加载订单数据
        ServiceOrderManage.findByIdWithProducts(orderId, function (data) {
          _this.setData({
            order: data
          })
        })
      });
    },

    onAssignPage: function (e) {
      const _this = this;
      const orderId = _this.data.order.id;
      ServiceOrderManage.assignPage(function (data) {
        _this.setData({
          assignPage: data,
          assignPageShowFlag: true
        })
      })
    },

    onAssign: function (e) {
      const _this = this;
      const orderId = _this.data.order.id;
      const branchCompanyStaffId = e.currentTarget.dataset.id;
      // 指派
      ServiceOrderManage.assign(orderId, branchCompanyStaffId, function (data) {
        // 加载订单数据
        ServiceOrderManage.findByIdWithProducts(orderId, function (data) {
          _this.setData({
            order: data,
            assignPageShowFlag: false
          })
        })
      });
    },

    onSubmitVerify: function (e) {
      const _this = this;
      const orderId = _this.data.order.id;
      ServiceOrderManage.submitVerify(orderId, function (data) {
        // 加载订单数据
        ServiceOrderManage.findByIdWithProducts(orderId, function (data) {
          _this.setData({
            order: data
          })
        })
      })
    },

    onVerifyPass: function (e) {
      const _this = this;
      const orderId = _this.data.order.id;
      ServiceOrderManage.verifyPass(orderId, function (data) {
        // 加载订单数据
        ServiceOrderManage.findByIdWithProducts(orderId, function (data) {
          _this.setData({
            order: data
          })
        })
      })
    },
    
    onSend: function (e) {
      const _this = this;
      const orderId = _this.data.order.id;
      ServiceOrderManage.send(orderId, function (data) {
        // 加载订单数据
        ServiceOrderManage.findByIdWithProducts(orderId, function (data) {
          _this.setData({
            order: data
          })
        })
      })
    },

    onFinish: function (e) {
      const _this = this;
      const orderId = _this.data.order.id;
      ServiceOrderManage.finish(orderId, function (data) {
        // 加载订单数据
        ServiceOrderManage.findByIdWithProducts(orderId, function (data) {
          _this.setData({
            order: data
          })
        })
      })
    }
    // onTerminal: function (e) {
    //   const _this = this;
    //   const orderId = _this.data.order.id;
    //   ServiceOrderManage.termial(orderId, function (data) {
    //     // 加载订单数据
    //     ServiceOrderManage.findByIdWithProducts(orderId, function (data) {
    //       _this.setData({
    //         order: data
    //       })
    //     })
    //   })
    // }

  }

})